﻿
    using System;


namespace DailyFx.UI
{
    /// <summary>
    ///		Helper class for encoding strings to regular expression patterns
    /// </summary>
    public static class RegExHelper 
	{
        /// <summary>
        ///		Regular expression pattern for valid name
        /// </summary>
        public const string NameRegEx = @"[A-Za-z_]\w*";

		/// <summary>
		///		Regular expression pattern for scoping (including dot)
		/// </summary>
		public const string ScopeRegEx = NameRegEx + @"\.";


        /// <summary>
        /// Regular expression pattern for namespace or namespace fragment
        /// </summary>
        public const string NsRegEx = "(" + ScopeRegEx + ")*";


        /// <summary>
        ///		Creates a named capture group with the specified regular expression 
        /// </summary>
        public static string GetGroup ( string groupName, string regEx)
		{
            return String.Concat(@"(?<", groupName, ">", regEx, ")");
        }

        /// <summary>
        ///		Converts a namespace (including wildcards) to a regular expression string
        /// </summary>
        public static string NsToRegEx ( string srcNs )
		{
            //Need to escape the "." as it's a special character in regular expression syntax
            var nsencoded = srcNs.Replace(".", @"\.");

            //Replace "*" wildcard with regular expression syntax
            nsencoded = nsencoded.Replace(@"*\.", NsRegEx);
            return nsencoded;
        }

        /// <summary>
        ///		Creates a capture group for a valid name regular expression pattern
        /// </summary>
        /// <param name="groupName">Name of capture group to create</param>
        /// <returns>Regular expression capture group with the specified group name</returns>
        public static string GetName ( string groupName )
		{
            return GetGroup(groupName, NameRegEx);
        }

        /// <summary>
        /// Creates a capture group for a namespace regular expression pattern
        /// </summary>
        /// <param name="groupName">Name of capture group to create</param>
        /// <returns>Regular expression capture group with the specified group name</returns>
        public static string GetNs ( string groupName ) 
		{
            return GetGroup (groupName, NsRegEx );
        }
    }
}